<?php

class Produk{

	private $kodeproduk;
	private $namaproduk;
    
	private $kategori;
    private $harga;
    private $kondisi;
	private $propinsi;
	private $kota;
	private $deskripsi;
	private $gambar;
    

    private $Host;
    private $MySQLUsername;
    private $MySQLPassword;
    private $Database;
    private $Conn;
	
	    public function Produk()
    {
        session_start();
        $this->Host = "localhost";
        $this->MySQLUsername = "root";
        $this->MySQLPassword = "";
        $this->Database = "fprbpl";

        $this->Connection();
        
        unset($this->Host);
        unset($this->MySQLUsername);
        unset($this->MySQLPassword);
        unset($this->Database);
    }


    public function Connection()
    {
        $this->Conn = @mysql_connect($this->Host,$this->MySQLUsername,$this->MySQLPassword);
        if($this->Conn)
        {
            mysql_select_db($this->Database) OR die('Could not select DB');
        }
        else
        {
            die(mysql_error());
        }  
    }
    
    public function Query($sql)
    {
        $result = mysql_query($sql); 
        if(!$result)
        {
            die(mysql_error());
        } 
        return $result;
    }
	
    
    public function Disconnect()
    {
        mysql_close($this->Conn);
    }
    
    //Escapes bad values for MySQL to prevent SQL injections.
    public function EscapeString($badstring)
    {
        if(!get_magic_quotes_gpc())
        {
            $goodstring = addslashes($badstring);
        }
        else
        {
            $goodstring = stripslashes($badstring);
        }
        $goodstring = mysql_real_escape_string($badstring);
        return $goodstring;
    }
    
	
 	public function Result_To_Array($result)
    {
        $result_array = array();

        for ($i=0; $row = mysql_fetch_array($result); $i++) 
        {
            $result_array[$i] = $row;
        }

        return $result_array;

    }
	
	/**public function Addgambar($gambar)
    {
		$ID = mysql_fetch_array($this->Query("SELECT MAX(ID_gambar)+1 from `gambar`"));
		$ID_gambar = $ID[0];
        $gambar = $this->EscapeString($gambar);
        $result = $this->Query("INSERT INTO `gambar` (ID_gambar,gambar) VALUES ('$ID_gambar','$gambar')");
    }**/
	
 public function AddProduk($kodeproduk,$kategori,$harga,$kondisi,$propinsi,$kota,$deskripsi,$gambar)
    {
	
		$ID = mysql_fetch_array($this->Query("SELECT MAX(ID_produk)+1 from `produk`"));
		$kodeproduk = $ID[0];
        $kategori = $this->EscapeString($kategori);
		/**$ID_k = mysql_fetch_array($this->Query("SELECT ID_kategori from `kategori` where nama_kategori='$kategori'"));
		$ID_kategori=$ID_k[0];*/
		$harga = $this->EscapeString($harga);
		$kondisi = $this->EscapeString($kondisi);
		$propinsi = $this->EscapeString($propinsi);
		$kota = $this->EscapeString($kota);
		$deskripsi = $this->EscapeString($deskripsi);
		$gambar= $this->EscapeString($gambar);
		
        $result = $this->Query("INSERT INTO `fprbpl`.`produk` (`kodeproduk`, `kategori`, `harga`, `kondisi`, `propinsi`, `kota`, `deskripsi`, `gambar`) VALUES ('001', 'Makanan', '40000', 'Baik', 'Jawa Timur', 'Surabaya', 'fkmnasola', ''));
		header('location:../detail_olahan.php'); 
    }
	
	
	}